<template>
  <el-dialog
    :model-value="modelValue"
    :title="dialogType === 'add' ? '新增' : '修改'"
    width="600px"
    @close="handleClose"
  >
    <el-form :model="form" :rules="rules" label-width="200px">
      <el-form-item label="主目录分类名称（中文）*" prop="nameCn">
        <el-input v-model="form.nameCn" placeholder="请输入中文名称" />
      </el-form-item>
      <el-form-item label="主目录分类名称（英文）*" prop="nameEn">
        <el-input v-model="form.nameEn" placeholder="请输入英文名称" />
      </el-form-item>
      <el-form-item label="购物网站排序*" prop="sortOrder">
        <el-input-number v-model="form.sortOrder" :min="1" :max="100" controls-position="right" />
      </el-form-item>
      <el-form-item label="状态*">
        <el-radio-group v-model="form.status">
          <el-radio label="active">启用</el-radio>
          <el-radio label="inactive">禁用</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item>
        <div style="color: #999; font-size: 12px;">任何项目编辑、编辑规则参照新增</div>
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="handleClose">取消</el-button>
      <el-button type="primary" @click="handleSubmit">确定</el-button>
      <el-button type="info" @click="testEcho">测试回显</el-button> <!-- 测试按钮 -->
    </template>
  </el-dialog>
</template>

<script setup>
import { ref, watch } from 'vue'
const props = defineProps({
  formData: Object,
  dialogType: String,
  modelValue: Boolean
})
const emit = defineEmits(['update:modelValue', 'submit'])

const form = ref({ ...props.formData })
watch(
  () => props.formData,
  (val) => {
    form.value = { ...val }
    console.log('watch触发，form已更新:', form.value)
  },
  { immediate: true } 
)

const rules = {
  nameCn: [{ required: true, message: '请输入中文名称', trigger: 'blur' }],
  nameEn: [{ required: true, message: '请输入英文名称', trigger: 'blur' }],
  sortOrder: [{ required: true, message: '请输入排序值', trigger: 'blur' }]
}

function handleSubmit() {
  emit('submit', form.value)
  emit('update:modelValue', false)
}
function handleClose() {
  emit('update:modelValue', false)
}
function testEcho() {
  console.log('props.formData:', props.formData)
  console.log('form:', form.value)
}
</script>

<style scoped>
.el-form-item .el-input__inner,
.el-form-item .el-input-number__inner {
  font-size: 14px;
}
.el-form-item__label {
  font-size: 13px;
  white-space: nowrap;
}
</style>